package pneuservis.frontend.web;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pneuservis.backend.services.TyreTypeServiceLocal;
import pneuservis.backend.to.TyreTypeTO;
 
/**
 *
 * @author Lukas Stevko
 */ 

@UrlBinding("/tyres/{$event}/{tyre.id}")
public class TyresActionBean implements ActionBean {
 
    final static Logger log = LoggerFactory.getLogger(TyresActionBean.class);
    @ValidateNestedProperties(value = {
        @Validate(on = {"save"}, field="id", required=true),
        @Validate(on = {"add"}, field="id", required=false),
        @Validate(on = {"add", "save"}, field = "rimSize", required = true, minvalue=1),
        @Validate(on = {"add", "save"}, field = "width", required = true, minvalue=1),
        @Validate(on = {"add", "save"}, field = "profile", required = true, minvalue=1),
        @Validate(on = {"add", "save"}, field = "price", required = true, minvalue=0),
        @Validate(on = {"add", "save"}, field = "manufacturer", required = true),
    })
    private TyreTypeTO tyre;

    @EJB(lookup="java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/TyreTypeService")
    private TyreTypeServiceLocal tyreTypeService;
    private ActionBeanContext context;
 
    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/admin/showTyre.jsp");
    }
 
    public List<TyreTypeTO> getTyres() {
        return tyreTypeService.getAllTyreTypes();
    }
 
   
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
 

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
    public Resolution add() {
        log.debug("add() tyre={}", tyre);
        tyreTypeService.createTyreType(tyre);
        return new RedirectResolution(this.getClass(), "all");
    }
 
    public TyreTypeTO getTyre() {
        return tyre;
    }
 
    public void setTyre(TyreTypeTO tyreTypeTO) {
        this.tyre = tyreTypeTO;
    }
    
    public Resolution delete() {
        log.debug("delete({})", tyre.getId());
        try {
            tyreTypeService.removeTyreType(tyre);
        } catch (EJBException e) {
            log.warn(e.getMessage());
            //TO DO javascript error window
            return new RedirectResolution(this.getClass(), "all");
        }
        
        return new RedirectResolution(this.getClass(), "all");
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadTyreFromDatabase() {
        String ids = context.getRequest().getParameter("tyre.id");
        if (ids == null) return;
        tyre = tyreTypeService.getTyreType(Long.parseLong(ids));
    }
 
    public Resolution edit() {
        log.debug("edit() tyre={}", tyre);
        return new ForwardResolution("/admin/editTyre.jsp");
    }
 
    public Resolution save() {
        log.debug("save() tyre={}", tyre);
        tyreTypeService.updateTyreType(tyre);
        return new RedirectResolution(this.getClass(), "all");
    }
    
        public Resolution storno() {
        log.debug("storno() tyre={}", tyre);
        return new RedirectResolution(this.getClass(), "all");
    }
}